const xlsx = require('node-xlsx');
const path = require('path');
const fs = require('fs-extra');
const glob = require('glob');

async function run() {
  const paths = await new Promise((resolve, reject) => {
    // 非自定义扩展
    glob(path.resolve(__dirname, '../resources/model/**/*.json'), (err, files) => {
      if (err) {
        reject(err);
      } else {
        resolve(files);
      }
    });
  });
  const typeMap = new Map();
  paths.forEach(pathStr => {
    pathStr = pathStr.replace(/\\/g, '/');
    pathStr = pathStr.substring(pathStr.indexOf('/resources/model') + 16);
    const name = path.basename(pathStr, '.json');
    typeMap.set(name, pathStr.replace('.json', ''));
  });
  const pathStr = path.resolve(__dirname, '../resources/xlsx/model-map.xlsx');
  const content = xlsx.parse(pathStr);
  const sheet = content[0].data;
  const map = new Map();
  for (let i = 1; i < sheet.length; i++) {
    const cls = sheet[i][0];
    const name = sheet[i][1];
    let type = sheet[i][2];
    if (type === 'JSONObject') {
      type = 'json';
    }
    if (cls === 'LookupModel') {
      continue;
    }
    if (!cls.startsWith('IPS') && cls.endsWith('Model')) {
      if (!map.has(cls)) {
        map.set(cls, {});
      }
      const data = map.get(cls);
      let scheme = '';
      // 自定义扩展
      {
        switch (type) {
          case 'ActionModel':
            scheme = '/action';
            break;
          case 'ApiDtoFieldModel':
            scheme = '/apiDtoField';
            break;
          case 'ApiDtoModel':
            scheme = '/apiDto';
            break;
          case 'ApiEntityModel':
            scheme = '/apiEntity';
            break;
          case 'ApiMethodModel':
            scheme = '/apiMethod';
            break;
          case 'ApiModel':
            scheme = '/api';
            break;
          case 'AppEntityModel':
            scheme = '/appEntity';
            break;
          case 'AppModel':
            scheme = '/app';
            break;
          case 'CtrlModel':
            scheme = '/ctrl';
            break;
          case 'DataSetModel':
            scheme = '/dataSet';
            break;
          case 'EntityModel':
            scheme = '/entity';
            break;
          case 'FieldModel':
            scheme = '/field';
            break;
          case 'PageModel':
            scheme = '/page';
            break;
          case 'RelationshipModel':
            scheme = '/relationship';
            break;
          case 'SystemModel':
            scheme = '/system';
            break;
          case 'WorkflowModel':
            scheme = '/workflow';
            break;
          case 'IPSAppView':
            scheme = '/page';
            break;
          case 'IPSControl':
            scheme = '/ctrl';
            break;
          case 'IPSAppDataEntity':
            scheme = '/appEntity';
            break;
        }
      }
      // 预置类型
      if ((!scheme || scheme === '') && typeMap.has(type)) {
        scheme = typeMap.get(type);
      }
      if (scheme && scheme !== '') {
        data[name] = { type: 'object', scheme };
      } else {
        data[name] = { type: type.toLowerCase() };
      }
    }
  }
  const writePath = path.resolve(__dirname, '../resources/model/extends');
  map.forEach((value, key) => {
    fs.writeFileSync(path.resolve(writePath, `${key}.json`), JSON.stringify(value, null, 2));
  });
}

run();
